package org.o14x.alpha.domain;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Manager for ExplorerTabs.
 * 
 * @author Olivier DANGREAUX
 */
public class ExplorerTabManager {
	/**
	 * Internal counter to manage ExplorerTab identifiers.
	 */
	private int idCounter;

	/**
	 * Map where ExplorerTab objects are stored.
	 */
	private Map<String, ExplorerTab> explorerTabMap;

	/**
	 * Creates a new instance of ExplorerTabManager.
	 */
	public ExplorerTabManager() {
		explorerTabMap = new LinkedHashMap<String, ExplorerTab>();
	}

	/**
	 * Gets the ExplorerTab with the given id.
	 * 
	 * @param id The id of an ExplorerTab.
	 * 
	 * @return The ExplorerTab associated with the id or null if no ExplorerTab was found.
	 */
	public ExplorerTab getExplorerTab(String id) {
		return explorerTabMap.get(id);
	}

	/**
	 * Gets a new instance of ExplorerTab.
	 * 
	 * @return A new instance of ExplorerTab.
	 */
	public ExplorerTab getNewExplorerTab() {
		String id = getNewId();
		ExplorerTab explorerTab = new ExplorerTab();
		explorerTab.setId(id);
		explorerTab.navigate(File.listRoots()[0]);

		explorerTabMap.put(id, explorerTab);
		return explorerTab;
	}

	/**
	 * Generates a new id for an ExplorerTab.
	 * 
	 * @return The generated id.
	 */
	private synchronized String getNewId() {
		idCounter++;
		return String.valueOf(idCounter);
	}

	/**
	 * Remove the ExplorerTab with the given id.
	 * 
	 * @param id The id of an ExplorerTab.
	 */
	public void removeExplorerTab(String id) {
		explorerTabMap.remove(id);
	}

	/**
	 * Counts managed ExplorerTabs
	 * 
	 * @return The number of ExplorerTabs.
	 */
	public int countExplorerTabs() {
		return explorerTabMap.size();
	}

	/**
	 * Returns the value of explorerTabMap.
	 *
	 * @return The value of explorerTabMap.
	 */
	public Map<String, ExplorerTab> getExplorerTabMap() {
		return explorerTabMap;
	}

	/**
	 * Sets the value of explorerTabMap.
	 *
	 * @param explorerTabMap The value of explorerTabMap to set.
	 */
	public void setExplorerTabMap(Map<String, ExplorerTab> explorerTabMap) {
		this.explorerTabMap = explorerTabMap;
	}
}
